//
//  BaseTableVC+Tool.m
//  Test
//
//  Created by mac on 2019/10/15.
//  Copyright © 2019 SZJ. All rights reserved.
//

#import "BaseTableVC+Tool.h"

@implementation BaseTableVC (Tool)

- (NSInteger)countSection{
    if (self.classStrArr == nil) {
        BOOL s = YES;
        if ([self.tableViewInterface respondsToSelector:@selector(isSystemStyle)]) {
            s = [self.tableViewInterface isSystemStyle];
        }
        if (!s) {
            return self.dataSouece.count;
        }else{
            if (self.tableView.style == UITableViewStylePlain){
                return 1;
            }else{
                return self.dataSouece.count;
            }
        }
        
    }else{
        if ([self.tableViewInterface respondsToSelector:@selector(sectionCount)]){
           return [self.tableViewInterface sectionCount];
        }
        if (self.tableView.style == UITableViewStylePlain){
             return 1;
        }
        
        return self.same ? self.classStrArr.count : self.dataSouece.count;
    }
}

- (NSInteger)countRow:(NSInteger)section{
    if (self.classStrArr == nil) {
        BOOL s = YES;
        if ([self.tableViewInterface respondsToSelector:@selector(isSystemStyle)]) {
            s = [self.tableViewInterface isSystemStyle];
        }
        if (!s) {
            if ([self.dataSouece[section] isKindOfClass:[NSArray class]]) {
                return [self.dataSouece[section] count];
            }else{
                return 1;
            }
        }else{
            if (self.tableView.style == UITableViewStylePlain){
                return self.dataSouece.count;
            }else{
                return 1;
            }
        }
    }else{
        if ([self.tableViewInterface respondsToSelector:@selector(rowsCount:)]){
             return [self.tableViewInterface rowsCount:section];
        }
        if (self.tableView.style == UITableViewStyleGrouped){
             return 1;
        }
        return self.same ? [self.classStrArr[section] count] : self.dataSouece.count;
    }
   
}

- (__kindof UITableViewCell *)getCell:(NSIndexPath *)indexPath{
    NSString *class = @"";
    BaseModel <BaseCellModelProtocol> *model =  [self getModel:indexPath];
    if (self.classStrArr == nil) {
       class = model.cellClassStr;
    }else{
       class = GetClassStr(self.classStrArr,indexPath);
    }
    
    BaseTableViewCell *  cell = [self.tableView dequeueReusableCellWithIdentifier:class];
    if (!cell) {
        [self.tableView registerNibReuseIdentifier:NSClassFromString(class)];
        cell = [self.tableView dequeueReusableCellWithIdentifier:class];
    }
    cell.indexPath = indexPath;
    cell.cellEvent = self.cellEvent;
    cell.cellTitle = self.cellTitle;
    cell.cellTitleIndex = self.cellTitleIndex;
    cell.cellTitles = self.cellTitles;
    cell.cellRoller = self.cellRoller;
    cell.cellTitleValue = self.cellTitleValue;
    cell.datasource_count = (int)self.dataSouece.count;

    if (model) {
        cell.model = model;
    }
    
    return cell;
}

- (CGFloat)cellHeight:(NSIndexPath *)indexPath{
     NSString *class = @"";
     BaseModel <BaseCellModelProtocol> *model = [self getModel:indexPath];
     if (self.classStrArr == nil) {
        class = model.cellClassStr;
     }else{
        class = GetClassStr(self.classStrArr,indexPath);
     }
    Class cellClass = NSClassFromString(class);
    return [cellClass cellHeight:model];
}

- (BaseModel <BaseCellModelProtocol> *)getModel:(NSIndexPath *)indexPath{
    BaseModel <BaseCellModelProtocol> * model;
    if (self.classStrArr == nil) {
         BOOL s = YES;
        if ([self.tableViewInterface respondsToSelector:@selector(isSystemStyle)])
        {
           s = [self.tableViewInterface isSystemStyle];
        }
        if (!s) {
            if ([self.dataSouece[indexPath.section] isKindOfClass:[NSArray class]]) {
                model = self.dataSouece[indexPath.section][indexPath.row];
            }else{
                model = self.dataSouece[indexPath.section];
            }
        }else{
            if (self.tableView.style == UITableViewStylePlain){
                model = self.dataSouece[indexPath.row];
            }else{
                model = self.dataSouece[indexPath.section];
            }
        }
    }else{
        if ([self.tableViewInterface respondsToSelector:@selector(modelForCell:)]){
            model = [self.tableViewInterface modelForCell:indexPath];
            
        }else{
            if (self.dataSouece.count == 0){
                return model;
            }else{
                if (self.tableView.style == UITableViewStylePlain){
                    model = self.dataSouece[indexPath.row];
                }
                if (self.tableView.style == UITableViewStyleGrouped)
                {
                    model = self.dataSouece[indexPath.section];
                }
            }
        }
    }
    
    return model;
}

@end
